package org.voyager.service.wx.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.voyager.service.wx.dao.WechatUserDao;
import org.voyager.service.wx.domain.WechatUserPO;

@Repository
public class WechatUserDaoJdbc implements WechatUserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    final static String Merge_User = "replace into wechat_users(openId, subscribe, nickname, sex, " +
            "language, city, province, country, head_img_url, subscribe_time, unionId, remark, groupId, " +
            "tagIds, privileges, subscribe_scene, qr_scene, create_time) values(?,?,?,?,?,?,?,?,?,?,?," +
            "?,?,?,?,?,?,now())";

    final static String U_Unsubscribe = "update wechat_users set subscribe = 0 where openId = ?";

    @Override
    public int mergeUser(WechatUserPO u) {
        return jdbcTemplate.update(Merge_User, new Object[]{u.getOpenId(), u.getSubscribe(),u.getNickname(), u.getSex(),
            u.getLanguage(),u.getCity(),u.getProvince(),u.getCountry(),u.getHeadImgUrl(),u.getSubscribeTime(),u.getUnionId(),
            u.getRemark(),u.getGroupId(),u.getTagIds(),u.getPrivileges(),u.getSubscribeScene(),u.getQrScene()});
    }

    @Override
    public int unsubscribe(String openId) {
        return jdbcTemplate.update(U_Unsubscribe, openId);
    }
}
